/**
 * @file route 执行中间件
 */
import { RouteObject, RouterObject, RequestLocation } from "../../index.d.js"

/** 获取当前请求对应的 route 配置
 *
 * @param {*} router defineRouter 解析后的路由配置
 * @param {*} reqLocation useRequestLocation 返回的地址对象
 * @param {*} strict 是否严格匹配 "/" 结尾
 * @return 返回对应的 route 配置
 */
export function useCurrentRoute(router: RouterObject, reqLocation: RequestLocation, strict?: boolean): RouteObject {
  const { pathname } = reqLocation

  let route = router[pathname]

  if (!strict && !router) {
    if (pathname.endsWith("/")) {
      route = router[pathname.replace(/\/$/, "")]
    } else {
      route = router[`${pathname}/`]
    }
  }

  return route
}
